<?php
// 这是系统自动生成的公共文件
use app\common\model\UrlModel;
use app\common\model\NewsModel;
use app\common\model\PhotosModel;
use app\common\model\NewsCategoryModel;
use app\common\model\MessagesModel;
use think\facade\View;
use think\facade\Db;

function get_current_lang(){
	$lang = \think\facade\Session::get('lang');
	if(empty($lang)){
		return 'zh';
	}else{
		return $lang;
	}
}

function static_path(){
    return '/static/index';
}

function setNavAvctive($path,$controllerName,$actionName=''){
    $current = $controllerName.'_'.$actionName;
    if(!empty($actionName)){
        if($path==$current){
            return 'class="active"';
        }
    }
    if($path==$controllerName){
        return 'class="active"';
    }
}


function get_news_list($cid,$limit=10){
     return $list = \app\common\model\NewsModel::listByCategory($cid,$limit);
}

function news_category_list($cid){
    $map = [];
    if(!empty($cid)){
        $map[] = ['pid','=',$cid];
    }
    $list = Db::name('news_category')->where( $map )->order('sort ASC')->select()->toArray();
    return $list;

}

function get_news_category_name($category_id){
	return \app\common\model\NewsCategoryModel::where('id',$category_id)->value('name');
}


function news_list_url($cid){
     return '/news/index/cid/'.$cid;
}

function notice_list_url($cid){
	if(!empty($cid)){
		return '/notice/index/cid/'.$cid;
	}else{
		return '/notice/index';
	}
}

function notice_category_list($cid){
	$map = [];
	if(!empty($cid)){
		$map[] = ['pid','=',$cid];
	}
	$list = Db::name('notice_category')->where( $map )->select()->toArray();
	return $list;

}

function get_notice_list($cid,$limit=10){
	return $list = \app\common\model\NoticeModel::listByCategory($cid,$limit);
}

function get_singlepage_info($var,$field='')
{
	$lang = get_current_lang();
    $info = \app\common\model\SinglepageModel::where('varname',$var)->where('lang',$lang)->find();
    if($info->status==0){
        $info->content = '';
        $info->description = '';
        $info->thumb = '';
    }
   if(!empty($field)){
    return $info[$field];
   	// return \app\common\model\SinglepageModel::where('varname',$var)->value($field);
   }
   return $info;
}

function get_news_count(){
	return $newsList = NewsModel::pageList([],12);
}
function comcontent($pl){
    $patterns = array ("/\[url\](.*?)\[\/url\]/","/\[qq\]([0-9]+)\[\/qq\]/","/\[img=?\]*(.*?)(\[\/img)?\]/","/\[strong\](.*?)\[\/strong\]/","/\[em\](.*?)\[\/em\]/","/\[del\](.*?)\[\/del\]/","/\[blockquote\](.*?)\[\/blockquote\]/","/\[u\](.*?)\[\/u\]/","/\[code\](.*?)\[\/code\]/"); 
	$replace = array ('<a rel="external nofollow" target="_blank" href="$1">$1 </a>','<a title="点击这里给我发消息" rel="external nofollow" target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=$1&site=qq&menu=yes"><img src="http://wpa.qq.com/pa?p=1:$1:52" alt="点击这里给我发消息" /></a>','<a target="_blank" href="$1"><img class="comment-img" src="$1" alt="" . basename("$1") . "\" /></a>','<b>$1</b>','<em>$1</em>','<del>$1</del>','<blockquote>$1</blockquote>','<u>$1</u>','<code>$1</code>'); 
	$pl=preg_replace($patterns, $replace, $pl);
    return $pl;
}
//留言列表
function get_message_list(){
	$messageList = \app\common\model\MessagesModel::where('status',1)->where('user_id',0)->order('create_time','desc')->select();
	foreach($messageList as $key=>$val){
	    $val->content = strip_tags($val->content);
	    if($val->pid==0){
	        $val->pidnewsid = UrlModel::getContactUrl($val->id);
	    }else{
	        $val->pidnewsid = UrlModel::getShowUrl($val->pid).'#comment-'.$val->id.'';
	    }
	    
	}
	return $messageList;
}
//About 更新记录
function get_about_update(){
	$upinfo = \app\common\model\UpdatelistModel::where('status',1)->order('create_time','desc')->select();
	return $upinfo;
}
//首页最新文章
function get_news_new_list(){
	$newsList = \app\common\model\NewsModel::where('status',1)->order('create_time','desc')->select();
	foreach($newsList as $key=>$val){
	    $val->category_name = NewsCategoryModel::where('id',$val->category_id)->value('name');
		$val->listurl = UrlModel::getShowUrl($val->category_id);
        if($val->id_domain == ''){
            $val->url = UrlModel::getShowUrl($val->id);
        }else{
            $val->url = UrlModel::getShowUrl($val->id_domain);
        }
// 		$val->url = '/news/'.$val->id.'.html';
	}
	return $newsList;
}
//最新相册
function get_photos_new_list(){
	return $photosList = PhotosModel::pageList([],6);
	$photosList = \app\common\model\PhotosModel::where('status',1)->order('sort','asc')->select();
	return $photosList;
}
//文章广告位
function get_advert_news_list(){
	$advertsList = \app\common\model\AdvertsModel::where('status',1)->where('category_id',2)->order('create_time','asc')->select();
	return $advertsList;
}
//首页广告位
function get_advert_index_list(){
	$advertsList = \app\common\model\AdvertsModel::where('status',1)->where('category_id',1)->order('create_time','asc')->select();
	return $advertsList;
}
//最新评论文章
function get_news_mess_list(){
	$messageList = \app\common\model\MessagesModel::where('status',1)->where('pid','<>',0)->where('user_id','=',0)->group('pid')->limit(6)->order('create_time','desc')->select();
	foreach($messageList as $key=>$val){
	    $val->title = NewsModel::where('status',1)->where('id',$val->pid)->value('title');
	    $val->ids = NewsModel::where('status',1)->where('id',$val->pid)->value('id');
	    $val->id_domain = NewsModel::where('status',1)->where('id',$val->pid)->value('id_domain');
	    $val->thumb = NewsModel::where('status',1)->where('id',$val->pid)->value('thumb');
	    $val->description = NewsModel::where('status',1)->where('id',$val->pid)->value('description');
        if($val->id_domain == ''){
            $val->url = UrlModel::getShowUrl($val->ids);
        }else{
            $val->url = UrlModel::getShowUrl($val->id_domain);
        }
// 		$val->url = '/news/'.$val->ids.'.html';
	}
	return $messageList;
}
//随机篇文章
function get_news_hota_list(){
	$newsList = \app\common\model\NewsModel::where('status',1)->limit(6)->orderRaw("rand() , id DESC")->select();
	foreach($newsList as $key=>$val){
        if($val->id_domain == ''){
            $val->url = UrlModel::getShowUrl($val->id);
        }else{
            $val->url = UrlModel::getShowUrl($val->id_domain);
        }
// 		$val->url = '/news/'.$val->id.'.html';
	}
    return $newsList;
}
//推荐位文章
function get_news_hot_list(){
	$newsList = \app\common\model\NewsModel::where('status',1)->where('recommended',1)->order('create_time','DESC')->select();
	foreach($newsList as $key=>$val){
        if($val->id_domain == ''){
            $val->url = UrlModel::getShowUrl($val->id);
        }else{
            $val->url = UrlModel::getShowUrl($val->id_domain);
        }
// 		$val->url = '/news/'.$val->id.'.html';
	}
	return $newsList;
}
//热门文章
function get_news_hots_list(){
	$newsList = \app\common\model\NewsModel::where('status',1)->order('clicks','desc')->select();
	foreach($newsList as $key=>$val){
        if($val->id_domain == ''){
            $val->url = UrlModel::getShowUrl($val->id);
        }else{
            $val->url = UrlModel::getShowUrl($val->id_domain);
        }
// 		$val->url = '/news/'.$val->id.'.html';
	}
	return $newsList;
}
//IT技术文章展示
function get_news_ITcoding_list(){
	$newsList = \app\common\model\NewsModel::where('status',1)->where('category_id',2)->order('create_time','desc')->select();
	foreach($newsList as $key=>$val){
        if($val->id_domain == ''){
            $val->url = UrlModel::getShowUrl($val->id);
        }else{
            $val->url = UrlModel::getShowUrl($val->id_domain);
        }
// 		$val->url = '/news/'.$val->id.'.html';
	}
	return $newsList;
}
//置顶位文章
function get_news_zhid_list(){
	$newsList = \app\common\model\NewsModel::where('status',1)->where('topped',1)->order('create_time','DESC')->select();
	foreach($newsList as $key=>$val){
        if($val->id_domain == ''){
            $val->url = UrlModel::getShowUrl($val->id);
        }else{
            $val->url = UrlModel::getShowUrl($val->id_domain);
        }
// 		$val->url = '/news/'.$val->id.'.html';
	}
	return $newsList;
}
//随机颜色
function randColor(){
    $colors = array();
    for($i = 0;$i<6;$i++){
        $colors[] = dechex(rand(0,15));
    }
    return implode('',$colors);
}
//文章标签
function get_news_tag_list(){
	$newsTag = \app\common\model\NewsModel::where('keywords','<>','')->where('status',1)->orderRaw('rand()')->group("keywords")->column('keywords');
	$result = implode(",",$newsTag);
    $str =  implode(",",array_diff(array_unique(explode(",",$result)),array("")));
    $TagArr = explode(',', $str);
    shuffle($TagArr);
    foreach($TagArr as $k=>$v){
        echo "<a href='"."/search.html?keyword=$v'>$v</a>";
    }
}
//文章分类栏目
function get_news_category_list(){
	$list = \app\common\model\NewsCategoryModel::where('status',1)->where('pid',0)->order('sort','asc')->select();
	foreach($list as $key=>$val){
        if($val->name == ''){
            $val->url = UrlModel::getNewsUrl($val->id);
        }else{
            $val->url = UrlModel::getNewsUrl($val->name);
        }
// 		$val->url = '/news/'.$val->id.'.html';
	}
	
	return $list;
}
//相册分类栏目
function get_photos_category_list(){
	$list = \app\common\model\PhotosCategoryModel::where('status',1)->order('sort','asc')->select();
	foreach($list as $key=>$val){
        if($val->name == ''){
            $val->url = UrlModel::getPhotosUrl($val->id);
        }else{
            $val->url = UrlModel::getPhotosUrl($val->name);
        }
		$val->newphotos = PhotosModel::where('category_id',$val->id)->limit(1)->order("create_time ASC")->value('thumb');
	}
	return $list;
}
//单页面栏目
function get_news_abouts_list(){
	$list = \app\common\model\SinglepageModel::where('status',1)->order('sort','asc')->select();
	foreach($list as $key=>$val){
		$val->url = '/'.$val->head_url.'.html';
	}
	return $list;
}
//导航栏旗下网站分类栏目
function get_qixia_url_list(){
	$list = \app\common\model\MenuinModel::where('pid',8)->where('status',1)->order('sort','asc')->select();
	return $list;
}

function get_link_list(){
	return \app\common\model\FriendlinkModel::where('status',1)->order('sort','ASC')->select();
}
//文章列表
function regroup_list($list){
	$data = [];
	$i = 0;
	$forKey = 0;
	foreach ($list as $key => $val) {
		$data[$forKey][] = $val;
		if($i>0){
			$forKey++;
		}
		if($i>0){
			$i=0;
		}else{
			$i++;
		}
        $val->newsMess = \app\common\model\MessagesModel::where('status',1)->where('pid',$val->id)->where('user_id',0)->count();

	}
	return $data;
}
